/*
 * Copyright 2016 Freelander
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.handong.handserialprogram.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 获取系统信息工具类
 * Created by Jun on 2016/4/21.
 */
public class SystemUtil {

    private static final String TAG = SystemUtil.class.getSimpleName();
    public static final int MAX_VOLUME = 15;
    public static final int DEFAULT_VOLUME = 8;

    /**
     * 获取当前应用的版本号
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            String version = packInfo.versionName;
            if (!TextUtils.isEmpty(version)) {
                return version;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取状态栏高度——方法1
     */
    public static int getStatusBarHeight(Context context) {

        Context temp = context.getApplicationContext();
        int statusBarHeight = -1;
        //获取status_bar_height资源的ID
        int resourceId = temp.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = temp.getResources().getDimensionPixelSize(resourceId);
        }
        JLog.d("WangJ", "状态栏-方法1:" + statusBarHeight);
        return statusBarHeight;
    }

    /**
     * 获取屏幕参数。
     *
     * @param context
     */
    public static void displayMetricsParams(Context context) {
        Context temp = context.getApplicationContext();
        DisplayMetrics metrics = temp.getResources().getDisplayMetrics();
        JLog.d("Display", "width:" + metrics.widthPixels + "|| height" + metrics.heightPixels);
        JLog.d("Display", "dpi:" + metrics.densityDpi);
        JLog.d("Display", "metrics.density:" + metrics.scaledDensity);
    }

    /**
     * 获取屏幕参数。
     *
     * @param context
     */
    public static float displayMetricsDpi(Context context) {
        Context temp = context.getApplicationContext();
        DisplayMetrics metrics = temp.getResources().getDisplayMetrics();
        return metrics.density;
    }

    public static String getPhoneIMEI(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String id = null;
        if (manager.getDeviceId() != null) {
            id = manager.getDeviceId();
        }
        //else {
        //    id = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        //}
        JLog.d(TAG, "TelephonyManager ID:" + id);
        return id;
    }

    public static String getPhoneNumber(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        return manager.getLine1Number();
    }

    public static String getSimSerialNumber(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        return manager.getSimSerialNumber();
    }

    public static String getDateString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date());
    }

    /**
     * HH：24小时制；hh：12小时制。
     *
     * @return
     */
    public static String getDateDetailsString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

    /**
     * 设置声音大小。
     *
     * @param context
     */
    public static void initVoiceLevel(Context context) {
        AudioManager manager = (AudioManager) context.getApplicationContext()
                .getSystemService(Context.AUDIO_SERVICE);
        manager.setStreamVolume(AudioManager.STREAM_MUSIC, DEFAULT_VOLUME, 0);
    }

    public static void updateVoiceLevel(Context context) {
        AudioManager manager = (AudioManager) context.getApplicationContext()
                .getSystemService(Context.AUDIO_SERVICE);
        int current = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (current == MAX_VOLUME) {
            manager.setStreamVolume(AudioManager.STREAM_MUSIC, DEFAULT_VOLUME, AudioManager.FX_FOCUS_NAVIGATION_UP);
        } else {
            manager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);
        }
    }
}
